<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 诉求发布用接口
 */
class Orderlist extends Api
{
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     *  全部分类
     * */

    public function getalltypes(){

        $list=Db::name('types')->order('weigh desc')->select();

        $this->success('全部分类', $list);


    }

    /**
     *  提交诉求
     * name: 12321
    imgs: http://location11.com/uploads/20240925/c3eaf93eb214af6aef4a416af0de9622.jpg,http://location11.com/uploads/20240925/dee79a3e12224f8ae740c7ccc1f24b63.png
    types_id: 3
     */
    public function userapply()
    {

        $userinfo=$this->auth->getUser();

        $user=Db::name('user')->where(['id'=>$userinfo['id']])->find();

        $data['user_id']=$user['id'];
        $data['name']=$this->request->param('name');

        $data['types_id']=$this->request->param('types_id');
        $data['addtime']=time();
        $data['nexttime']=strtotime('+7 day');


        foreach ($data as $key=>$v){
            if (empty($v)){
                $this->error('请填写完整信息！');
            }

        }

        $data['images']=$this->request->param('imgs');

        $data['group_id']=$user['group_id'];


        $info=Db::name('categorys')->where(['id'=>$user['group_id']])->find();

        $p3=Db::name('categorys')->where(['id'=>$info['pid']])->find();

        $p2=Db::name('categorys')->where(['id'=>$p3['pid']])->find();

        $p1=Db::name('categorys')->where(['id'=>$p2['pid']])->find();

        $data['p4']=$info['user_id'];
        $data['p3']=$p3['user_id'];
        $data['p2']=$p2['user_id'];
        $data['p1']=$p1['user_id'];
        $data['level']=4;

        Db::name('shenhe')->data($data)->insert();


        $this->success('提交诉求成功！');


    }

    /** 我的诉求 */
    public function getmysuqiulist(){

        $status=$this->request->param('status');
        $user=$this->auth->getUser();


        $lists= Db::name('shenhe')
            ->alias('a')
            ->join('fa_types b','a.types_id = b.id')
            ->where(['a.user_id'=>$user['id']])
            ->where(['a.status'=>$status])
            ->field('a.*,b.name as gname')
            ->select();


        foreach ($lists as &$key){

            $key['addtime']=date('Y-m-d H:i',$key['addtime']);

            $key['status_v']=getstatus($key['status']);

            $key['imgs']=getimgs($key['images']);
        }


        $this->success('我的诉求',$lists);


    }

    /*
     * 删除此项目
     * */
    public function deletesuqiu(){

        $id=$this->request->param('id');

         Db::name('shenhe')
            ->where(['id'=>$id])
            ->delete();

        $this->success('删除成功');

    }

    /**
     *  诉求详情
     * */
    public function getsuqiuinfobyid(){

        $id=$this->request->param('id');

        $info=Db::name('shenhe')
            ->where(['id'=>$id])
            ->find();

        $types=Db::name('types')->where(['id'=>$info['types_id']])->find();

        $info['img_arr']=getimgs($info['images']);

        $info['status_v']=getstatus($info['status']);

        $info['types_name']=$types['name'];

        $info['addtime']=date('Y-m-d H:i',$info['addtime']);


        $rets=Db::name('shenhe_log')
            ->where(['shenhe_id'=>$info['id']])
            ->where(['status'=>1])
            ->find();



        if ($rets){
            $info['ret_name']=$rets['title'];
            $info['ret_img']=getimgs($rets['images']);
            $info['ret_time']=date('Y-m-d H:i',$rets['addtime']);
        }

        $user=Db::name('user')->where(['id'=>$info['user_id']])->find();
        $info['nickname']=$user['nickname'];
        $info['avatar']=$user['avatar'];


        $this->success('诉求详情',$info);

    }


    /**
    *  是否满意
     */
    public function manyi(){

        $id=$this->request->param('id');
        $index=$this->request->param('index');

        Db::name('shenhe')->where(['id'=>$id])
            ->update(['like'=>$index,'status'=>2]);

        $this->success('是否满意');


    }

    //全部行
    public function getalllistbykey(){
        $page=$this->request->param('page');
        $type_id=$this->request->param('type_id');
        $key=$this->request->param('key');

        $map['a.see']=['eq',1];

        if ($key){
            $map['a.name']=['like','%'.$key.'%'];
        }
        if ($type_id){
            $map['a.types_id']=['eq',$type_id];
        }

        $limit=($page*3) . ',3';

        $lists= Db::name('shenhe')
            ->alias('a')
            ->join('fa_types b','a.types_id = b.id')
            ->where($map)
            ->limit($limit)
            ->field('a.*,b.name as gname')
            ->select();

        foreach ($lists as &$key){

            $key['addtime']=date('Y-m-d H:i',$key['addtime']);

            $key['status_v']=getstatus($key['status']);

            $key['imgs']=getimgs($key['images']);

            $user=Db::name('user')->where(['id'=>$key['user_id']])->find();


            $key['nickname']=$user['nickname'];
            $key['avatar']=$user['avatar'];

        }


        $this->success('我的诉求',$lists);

    }





}